package vip.xiaonuo.smzq.modular.lifecycle.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Component;
import vip.xiaonuo.smzq.modular.lifecycle.vo.common.TreeFilterVO;

import java.util.List;

/**
 * HTOOL TREE 扩展
 * @author GEOSCENE_YUYF
 */
@Component
public class HutoolTreeExtend {

    /**
     * 根据树节点名称过滤树节点
     * @param trees 所有的树几点
     * @param name 过滤的名称
     * @param keywordNoVerifyIds 关键字不验证的id列表
     * @return 过滤出的树节点列表
     */
    public <T> List<Tree<T>> getTreeFilter(List<Tree<T>> trees, String name, T buildId,List<T> keywordNoVerifyIds){
        List<Tree<T>> filters = CollUtil.newArrayList();
//        根据关键字查询出树
        recursionFilterTreesByName(trees,filters,name,keywordNoVerifyIds);
        List<TreeFilterVO<T>> treeFilterVOS = CollUtil.newArrayList();
        filters.forEach(ele->{
            TreeFilterVO treeFilterVO = new TreeFilterVO();
            treeFilterVO.setIds(TreeUtil.getParentsId(ele, true));
            treeFilterVO.setNames(Convert.convert(new TypeReference<List<String>>() {},TreeUtil.getParentsName(ele, true)));
            treeFilterVOS.add(treeFilterVO);
        });
        return TreeUtil.build(getFilterTreeNodes(treeFilterVOS),buildId);
    }




    /**
     * 得到过滤的nodes
     * @param treeFilterVOS 生成树顾虑vos
     * @return
     */
    private <T> List<TreeNode<T>> getFilterTreeNodes(List<TreeFilterVO<T>> treeFilterVOS){
        List<TreeNode<T>> result = CollUtil.newArrayList();
        for (TreeFilterVO treeFilterVO:treeFilterVOS) {
            for (int i = 0; i < CollUtil.size(treeFilterVO.getNames()); i++) {
                TreeNode<T> node = new TreeNode<T>();
                node.setId((T)CollUtil.get(treeFilterVO.getIds(),i));
                node.setName((String)CollUtil.get(treeFilterVO.getNames(),i));
                node.setParentId((T)CollUtil.get(treeFilterVO.getIds(),i+1));
                node.setWeight(3);
                result.add(node);
            }
        }


        return result;
    }

    /**
     * 得到模糊匹配名车给的树节点列表
     * @param trees 所有的树节点
     * @param filters  过滤完了的树节列表
     * @param name 名称
     * @return void
     */
    private <T> void recursionFilterTreesByName(List<Tree<T>> trees,List<Tree<T>> filters,String name,List<T> keywordNoVerifyIds){
        for (Tree<T> tree:trees) {
            if(CollUtil.size(tree.getChildren())!=0){
                addTreeToFilters(filters,tree,name,keywordNoVerifyIds);
                recursionFilterTreesByName(tree.getChildren(),filters,name,keywordNoVerifyIds);
            }else{
                addTreeToFilters(filters,tree,name,keywordNoVerifyIds);
            }
        }
    }

    private <T> void addTreeToFilters(List<Tree<T>> filters,Tree<T> tree,String name,List<T> keywordNoVerifyIds){
        if(StrUtil.contains(tree.getName(), name) || CollUtil.contains(keywordNoVerifyIds,tree.getId())){
            filters.add(tree);
        }
    }

    /**
     * 通过id在树列表中得到树
     * @param trees 树列表
     * @param id id值
     * @return
     */
    public Tree<String> getTreeById(List<Tree<String>> trees,String id){
        Tree<String> result = null;
        for (Tree<String> tree:trees) {
            Tree<String> tree1 =  TreeUtil.getNode(tree,id);
            if(ObjectUtil.isNotNull(tree1)){
                result = tree1;
                break;
            };
        }
        return result;
    }
}
